using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CountDownDisplay : MonoBehaviour
{
    public RawImage ImgHundreds;
    public RawImage ImgTens;
    public RawImage ImgOnes;

    public bool StartCountRightNow = false;
    public bool IgnorePause = false;
    public bool DisplaySE = false;

    private bool bStartCountDown = false;
    private float RemainTime = 0.0f;

    // Start is called before the first frame update
    void Start()
    {
        if (StartCountRightNow)
        {
            OnStartCountDown();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!bStartCountDown)
            return;

        if (!IgnorePause && GameManager.Instance.isPause)
            return;

        float GameRemainTime = GameManager.Instance.RemainTime;
        if (RemainTime == GameRemainTime)
            return;

        RemainTime = GameRemainTime;

        if (0 == RemainTime) 
        {
            OnStopCountDown();
        }

        if (RemainTime < 10)
        {
            if (DisplaySE)
                AudioManager.Instance.PlaySE("SE_03_Button");
        }

        UpdateCountDisplay();
    }
    void UpdateCountDisplay()
    {
        int hundreds = Mathf.FloorToInt(RemainTime / 100);
        int tens = Mathf.FloorToInt((RemainTime - hundreds * 100) / 10);
        int ones = Mathf.FloorToInt(RemainTime % 10);
        if (null != ImgHundreds)
            ImgHundreds.texture = Resources.Load<Texture>(string.Format("Texture/Img_Number_{0}", hundreds));

        if (null != ImgTens)
            ImgTens.texture = Resources.Load<Texture>(string.Format("Texture/Img_Number_{0}", tens));

        if (null != ImgOnes)
            ImgOnes.texture = Resources.Load<Texture>(string.Format("Texture/Img_Number_{0}", ones));
    }

    public void OnStartCountDown()
    {
        gameObject.SetActive(true);

        float GameRemainTime = GameManager.Instance.RemainTime;
        RemainTime = GameRemainTime;
        UpdateCountDisplay();

        bStartCountDown = true;
    }
    public void OnStopCountDown()
    {
        bStartCountDown = false;

        gameObject.SetActive(false);
    }
}
